﻿'Created by Chris Harris aka InModWeTrust360
'www.youtube.com/InModWeTrust360
'www.youtube.com/TheTerraria Wiki
'All code used in this application was created by me. If you use ANY
'of this code please provide credit where credit is due!
'If you are viewing this code via Open Source I hope you learn some
'things about coding and if you are viewing this because you decomplied
'my source then shame on you! :)
'I have left notes beside most of my code so you can see what the application
'is executing so you can learn do NOT just copy. You will get no where.
'© InModWeTrust360™, 2011

Imports System.Net
Public Class Main

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'adds text to log
        Me.ListBox1.Items.Add("(" + DateTime.Now & ") - Create Server Form Launched")
        Create.Show()
        Button1.Enabled = False
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        'simple yet effective way to add text to the log if a file is opened
        If TextBox1.TextLength > 1 Then
            Me.ListBox1.Items.Add("(" + DateTime.Now & ") - TerrariaServer.exe Detected")
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.ListBox1.Items.Add("(" + DateTime.Now & ") - Browsing for TerrariaServer.exe")
        'sets the OFD's Title
        OpenFileDialog1.Title = "Please Select the TerrariaServer.exe"
        'fills in the textbox of the OFD
        OpenFileDialog1.FileName = "Choose File..."
        'sets what files are able to be chosen(For Easy Browsing)
        OpenFileDialog1.Filter = ".exe Files (*.exe)|*.exe|All Files (*.*)|*.*"
        'make the initial directory the Terraria Foler
        OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\My Games\Terraria"
        'makes the OFD visible (This should always be last because you need to set the parameters first)
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        'declares strm as Stream reader
        Dim strm As System.IO.Stream
        'takes the open files path
        strm = OpenFileDialog1.OpenFile()
        'takes the file path and exports text to fill in the text box
        TextBox1.Text = OpenFileDialog1.FileName.ToString()
        Button1.Enabled = True
        Button2.Enabled = False
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Timer1.Start()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Timer2.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Increment(1)

        If ProgressBar1.Value = 5 Then
            AppActivate("Terraria Server:")
            SendKeys.Send(ListBox2.Text)
            SendKeys.Send("{ENTER}")
            Me.ListBox1.Items.Add("(" + DateTime.Now & ") - " + ListBox2.Text + " Command Executed")
            Timer1.Stop()
            ProgressBar1.Value = 0
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        ProgressBar1.Increment(1)

        If ProgressBar1.Value = 5 Then
            AppActivate("Terraria Server:")
            SendKeys.Send(ListBox3.Text + " " + TextBox2.Text)
            SendKeys.Send("{ENTER}")
            Me.ListBox1.Items.Add("(" + DateTime.Now & ") - " + ListBox3.Text + " " + TextBox2.Text + " Command Executed")
            Timer2.Stop()
            ProgressBar1.Value = 0
        ElseIf ListBox3.Text = "password" Then
            TextBox6.Text = TextBox2.Text
        End If
    End Sub

    Private Sub Main_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        'gets the process from the task list and kills the server
        Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("TerrariaServer")

        For Each p As Process In pProcess
            p.Kill()
        Next
        'makes sure the application is completely killed (Closes process and application)
        Application.Exit()
    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'reads the txt file on my server so I can change the MOTD when needed
        Dim wc As New WebClient()
        ToolStripStatusLabel4.Text = wc.DownloadString("http://zctexthost.webs.com/MOTD.txt")
        'fades the application in (Cool effect)
        For FadeIn = 0.0 To 1.1 Step 0.1
            Me.Opacity = FadeIn
            Me.Refresh()
            Threading.Thread.Sleep(100)
        Next
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Process.Start("http://store.steampowered.com/app/105600/")
    End Sub

    Private Sub Main_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'fades the application out
        For FadeOut = 90 To 10 Step -10
            Me.Opacity = FadeOut / 100
            Me.Refresh()
            Threading.Thread.Sleep(50)
        Next
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        Process.Start("http://www.terrariaonline.com/members/inmodwetrust360.46342/")
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Me.ListBox1.Items.Add("(" + DateTime.Now & ") - Video Tutorial Launched")
        Video.Show()
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        'check for updates (Need help then contact me)
        Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://zctexthost.webs.com/Terraria%20Server%20Version.txt")
        Dim response As System.Net.HttpWebResponse = request.GetResponse()

        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())

        Dim newestversion As String = sr.ReadToEnd()
        Dim currentversion As String = Application.ProductVersion

        If newestversion.Contains(currentversion) Then
            MessageBox.Show("You Currently Have The Most Up-to-Date Version!", "Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("New Version Available! Click Ok To Download", "Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Process.Start("http://dl.dropbox.com/u/18268207/Terraria%20Server%20Client%20setup.exe")
        End If

        Me.ListBox1.Items.Add("(" + DateTime.Now & ") - Checked for Updates")
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Edit.Show()
        Me.ListBox1.Items.Add("(" + DateTime.Now & ") - Banlist Editor Launched")
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Backup.Show()
        Me.ListBox1.Items.Add("(" + DateTime.Now & ") - World Backup Launched")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Config.Show()
        Me.ListBox1.Items.Add("(" + DateTime.Now & ") - Server Config Editor Launched")
    End Sub

    Private Sub Button7_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Timer3.Start()
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        ProgressBar1.Increment(1)

        If ProgressBar1.Value = 5 Then
            AppActivate("Terraria Server:")
            SendKeys.Send(TextBox8.Text)
            SendKeys.Send("{ENTER}")
            Me.ListBox1.Items.Add("(" + DateTime.Now & ") - " + TextBox8.Text + " Command Executed")
            Timer3.Stop()
            ProgressBar1.Value = 0
        End If
    End Sub
End Class
